package com.jhgsys.internal.accident.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.accident.entity.AccidentReportTimeline;
import com.jhgsys.internal.accident.mapper.AccidentReportTimelineMapper;
import com.jhgsys.internal.accident.service.IAccidentReportTimelineService;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author zjb
 * @date 2024-09-13 10:24:35
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AccidentReportTimelineServiceImpl extends ServiceImpl<AccidentReportTimelineMapper, AccidentReportTimeline> implements IAccidentReportTimelineService {

    /**
     * 查询（分页）
     * @author zjb
     * @date 2024-09-13 10:24:35
     * @param request QueryRequest
     * @param accidentReportTimeline accidentReportTimeline
     * @return IPage<AccidentReportTimeline>
    */
    @Override
    public IPage<AccidentReportTimeline> findAccidentReportTimelinePage(QueryRequest request, AccidentReportTimeline accidentReportTimeline) {
        LambdaQueryWrapper<AccidentReportTimeline> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportTimeline::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<AccidentReportTimeline> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zjb
     * @date 2024-09-13 10:24:35
     * @param accidentReportTimeline accidentReportTimeline
     * @return List<AccidentReportTimeline>
     */
    @Override
    public List<AccidentReportTimeline> findAccidentReportTimelineList(AccidentReportTimeline accidentReportTimeline) {
	    LambdaQueryWrapper<AccidentReportTimeline> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportTimeline::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zjb
    * @date 2024-09-13 10:24:35
    * @param id
    * @return AccidentReportTimeline
    */
    @Override
    public AccidentReportTimeline findAccidentReportTimelineById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zjb
     * @date 2024-09-13 10:24:35
     * @param accidentReportTimeline accidentReportTimeline
     */
    @Override
    @Transactional
    public boolean createAccidentReportTimeline(AccidentReportTimeline accidentReportTimeline) {
        return this.save(accidentReportTimeline);
    }

    /**
     * 修改
     * @author zjb
     * @date 2024-09-13 10:24:35
     * @param accidentReportTimeline accidentReportTimeline
     */
    @Override
    @Transactional
    public boolean updateAccidentReportTimeline(AccidentReportTimeline accidentReportTimeline) {
        return this.updateById(accidentReportTimeline);
    }

    /**
     * 逻辑删除
     * @author zjb
     * @date 2024-09-13 10:24:35
     * @param accidentReportTimeline accidentReportTimeline
     * @param accidentReportTimelineIds accidentReportTimelineIds
     */
    @Override
    @Transactional
    public boolean deleteAccidentReportTimeline(AccidentReportTimeline accidentReportTimeline,String accidentReportTimelineIds) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(accidentReportTimelineIds.split(StringPool.COMMA));

        accidentReportTimeline.setDeletedBy(user.getUserName());
        accidentReportTimeline.setDeletedTime(new Date());
        accidentReportTimeline.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(accidentReportTimeline,new LambdaQueryWrapper<AccidentReportTimeline>().in(AccidentReportTimeline::getReportTimelineId,list));
        if (update > 0 ){
            result = true;
        }
        return result;
	}

    /**
    * 物理删除
    * @author zjb
    * @date 2024-09-13 10:24:35
    * @param accidentReportTimelineIds accidentReportTimelineIds
    */
    @Override
    @Transactional
    public boolean physicallyDeleteAccidentReportTimeline(String accidentReportTimelineIds) {
        List<String> list = Arrays.asList(accidentReportTimelineIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportTimeline> wapper = new LambdaQueryWrapper<>();
        wapper.in(AccidentReportTimeline::getReportTimelineId,list);
        return this.remove(wapper);
    }

    /**
     * 根据事故报告id删除
     * @author admin
     * @date 2024-07-29 15:56:15
     * @param reportIds
     */
    @Override
    @Transactional
    public void deleteByReportIds(String reportIds) {
        List<String> list = Arrays.asList(reportIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportTimeline> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(AccidentReportTimeline::getReportId,list);
        this.remove(wrapper);
    }
}
